package com.springboot.controller.blogview;

import cn.hutool.core.util.StrUtil;
import com.springboot.config.log.SpringBootRequestLog;
import com.springboot.config.controller.BaseController;
import com.springboot.jwt.dto.RequestComm;
import com.springboot.jwt.interfaces.JwtToken;
import com.springboot.returndata.dto.blog.BlogCommentDto;
import com.springboot.service.blog.BlogUserService;
import com.springboot.util.enums.RespErrorCode;
import com.springboot.util.http.ResponseBuilder;
import com.springboot.util.http.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName BlogOperateController
 * @Description
 * @Author zyj
 * @Date 2023/5/24 13:23
 * @Version 1.0
 **/
@RestController
@RequestMapping("/blogOperate")
@Slf4j
@SpringBootRequestLog
@Tag(name = "博客页用户登录操作类")
@JwtToken(requestTokenType = RequestComm.REQUEST_TOKEN_TYPE_BLOGUSER)
public class BlogOperateController  extends BaseController {

    @Resource
    private BlogUserService blogUserService;

    @GetMapping(value = "/logOut")
    @Operation(description = "退出登录")
    public ResultResponse<String> logOut() {
        try {
            String token = getToken();
            if(StrUtil.isNotEmpty(token)){
                blogUserService.logOut(token);
            }
            return ResponseBuilder.success();
        } catch (Exception e) {
            log.error("homeContent error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }


    @PostMapping(value = "/blogComment")
    @Operation(description = "评论")
    public ResultResponse<String> blogComment(@Validated @RequestBody BlogCommentDto blogCommentDto) {
        try {
            blogCommentDto.setCommentUserId(Integer.parseInt(super.getUserId()));
            blogUserService.blogComment(blogCommentDto);
            return ResponseBuilder.success();
        } catch (Exception e) {
            log.error("blogComment error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.BLOG_COMMENT_ERROR.getValue(), RespErrorCode.BLOG_COMMENT_ERROR.getDescripe());

    }
}
